package com.lions.client.view.mappanel;

import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.lions.client.model.Model;
import com.lions.client.model.Module;
import com.lions.client.model.ModuleList;
/**
 * A class to display the mars map with icons
 * @author Tony Mengelkoch
 *
 */
public class MapPanel extends ScrollPanel {
	
	public MapPanel(Model m){
		super();
		height = 50;
		width = 100;
		//this.setPixelSize(1100, 550);
		map = new Grid(height, width);
		map.setStyleName("gwt-Grid"); 
		map.setCellPadding(0);
		map.setCellSpacing(0);
		model = m;
		this.add(map);
		//marking unusable area
		for(int i = 0; i < 10; i++){
			for(int j = 40; j < 50; j++){
				map.setWidget(i, j, new Image("images/UNUSABLE.jpg"));
			}
		}
		
		
		
	}
	
	/**
	 * Redraws all of the map panels images.
	 */
	public void populate(){
		int yInverter = 0;
		map.clear();
		for(int i = 0; i < 10; i++){
			for(int j = 40; j < 50; j++){
				map.setWidget(i, j, new Image("images/UNUSABLE.jpg"));
			}
		}
		ModuleList moduleList = model.getModuleList();
		Module[][] container = new Module[height][width];
		for(Module module : moduleList){
			if((module.getX() < 100) && (module.getY() < 50))
			container[module.getY()][module.getX()] = module;
		}
		for (int row = 49; row >= 0; row--) {
			for (int col = 99; col >= 0; col--) {
				if (container[row][col] != null) {
					//map.getCellFormatter().setStyleName(row, col, "testitem");
					map.setWidget(yInverter, col, new Image(
							"images/" + container[row][col].getType() + ".jpg"));
				}
			}
			yInverter++;
		}
	}
	
	public void clearMapGrid(){
		for(int rows = 0; rows < height; rows++){
			for(int col = 0; col < width; col++){
				map.setWidget(rows, col, null);
			}
		}
	}
	public int getMapWidth() {
		return width;
	}
	
	public int getMapHeight() {
		return height;
	}
	
	public Grid getMap(){
		return map;
	}
	private Grid map;
	private Model model;
	private int height;
	private int width;
}
